//
// Created by admin on 2022/6/16.
// 200. 岛屿数量: https://leetcode.cn/problems/number-of-islands/
//
#include <vector>
using namespace std;

// 深度优先级遍历
class Numslands {
public:
    int numIslands(vector<vector<char>>& grid) {
        int res = 0;
        for (int i = 0; i < grid.size(); ++i) {
            for (int j = 0; j < grid[0].size(); ++j) {
                if (grid[i][j] == '1') {
                    dfs(grid,i,j);
                    res++;
                }
            }
        }
        return res;
    }

private:
    void dfs(vector<vector<char>>& grid, int row, int col) {
        if (row >= grid.size() || row < 0 || col >= grid[0].size() || col < 0) return;
        if (grid[row][col] != '1') return;
        grid[row][col] = '2';
        dfs(grid,row+1, col);
        dfs(grid,row-1, col);
        dfs(grid,row, col+1);
        dfs(grid,row, col-1);
    }
};